﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CXMLStudioIDE.DataAccessLayer;
using CXMLStudioIDE.Common;
using CXMLStudioIDE.Common.Entities;
using System.Threading;

namespace CXMLStudioIDE.Forms
{
    public partial class frmOpenSolution : Form
    {
        MainForm m_parent;

        public frmOpenSolution(MainForm frm_parent)
        {
            InitializeComponent();
            m_parent = frm_parent;
            frm_parent.EnabledMenuItem("menuItemCloseSolution", true);
            frm_parent.EnabledMenuItem("menuItemSaveSolution", true);
            frm_parent.EnabledMenuItem("menuItemSaveAll", true);
        }

        private void frmOpenSolution_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cXML_Object_StoreDataSet.CXMLUserSolutions' table. You can move, or remove it, as needed.
            CXMLStudioIDE.DataAccessLayer.SolutionManagerDAL dal = new SolutionManagerDAL();
            dsOpenSolution = dal.GetSolutionsForUser(m_parent.m_CXMLUser);
            dgvOpenSolution.DataSource = dsOpenSolution.Tables[0];
            if (dgvOpenSolution.Columns.Count != 0)
            {
                AdjustColumnOrder();
            }

        }
        private void AdjustColumnOrder()
        {
            dgvOpenSolution.Columns["UserID"].Visible = false;
            dgvOpenSolution.Columns["NetworkUserID"].DisplayIndex = 1;
            dgvOpenSolution.Columns["RoleID"].Visible = false;
            dgvOpenSolution.Columns["RoleName"].Visible = false;
            dgvOpenSolution.Columns["SolutionID"].Visible = false;
            dgvOpenSolution.Columns["SolutionName"].DisplayIndex = 0;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            int i_SolutionID = (int)dgvOpenSolution.SelectedRows[0].Cells[4].Value;
            string sZ_SolutionName = dgvOpenSolution.SelectedRows[0].Cells[5].Value.ToString();
            Solution s = new Solution(i_SolutionID, sZ_SolutionName);
            if(i_SolutionID >= 0){
                m_parent.Cursor = Cursors.AppStarting;
                m_parent.m_SolutionManager.OpenSolution(s);
                m_parent.Cursor = Cursors.Default;
                m_parent.UpdateMainForm();
                m_parent.Text = s.Name + " - CXMLStudioIDE";
                m_parent.EnabledMenuItem("menuItemSolution", true);
                this.Cursor = Cursors.Default;
                this.Dispose();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dgvOpenSolution_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnOpen_Click(sender, e);
        }

        private void dgvOpenSolution_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnOpen_Click(sender, e);
            }
        }

    }
}
